package com.smart.controller.app;

import java.util.Map;
import java.util.List;
import com.smart.util.DateUtil;
import java.time.LocalDateTime;
import com.smart.entity.SysUser;
import javax.annotation.Resource;
import lombok.AllArgsConstructor;
import io.swagger.annotations.Api;
import com.smart.vo.SsAttendanceVO;
import com.smart.service.CarService;
import com.smart.entity.SsAttendance;
import com.smart.common.util.Messages;
import com.smart.req.app.SsAttendanceReq;
import io.swagger.annotations.ApiOperation;
import com.smart.common.util.SysUserUtils;
import com.smart.controller.BaseController;
import com.smart.service.AttendanceService;
import org.springframework.beans.BeanUtils;
import com.baomidou.mybatisplus.extension.api.R;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lgb
 * @date 2020年6月10日 下午2:13:47
 * Copyright (C) 深圳工勘科技
 * @说明: 智慧工地-考勤管理
 **/
@AllArgsConstructor
@RestController("appAttendanceController")
@RequestMapping("/app/ssattendance")
@Api(value="attendance",tags="智慧工地-考勤管理")
public class AttendanceController extends BaseController{

	@Resource
	private AttendanceService attendanceService;
	@Resource
	private CarService carService;
	/**
     * 分页查询
     * @param page         分页对象
     * @param appSsAttendance 智慧工地-作业记录表
     * @return
     **/
    @ApiOperation(value="分页查询", notes = "分页查询")
    @GetMapping("/page")
    public Messages<List<SsAttendance>> getSsAttendancePage(
    		String createTime,
    		@RequestParam(name="pageNum")Integer pageNum,
			@RequestParam(name="pageSize")Integer pageSize) {
    		List<SsAttendance> page = attendanceService.findPage(createTime, pageNum, pageSize);
            return Messages.success(page);
     }
	
    /**
     * @return 获取最新打卡记录
     **/
    @ApiOperation(value = "获取最新打卡记录", notes = "获取最新打卡记录")
    @GetMapping("/attendance/latest")
    public Messages<SsAttendanceVO> getLatestSsAttendance() {
        SsAttendanceVO vo = attendanceService.getLatestSsAttendance();
        return Messages.success(vo);
    }

    /**
     * 通过id查询智慧工地-作业记录表
     * @param id id
     * @return R
     **/
    @ApiOperation(value="通过id查询",notes="通过id查询")
    @GetMapping("/{id}")
    public R<SsAttendance> getById(@PathVariable("id")Long id) {
        return R.ok(attendanceService.findById(id));
    }

    /**
     * 新增智慧工地-作业记录表
     * @param dto 智慧工地-作业记录表
     * @return R
     **/
    @ApiOperation(value="新增智慧工地-作业记录表",notes="新增智慧工地-作业记录表")
    @PostMapping
    public Messages<String> save(@RequestBody SsAttendanceReq dto) {
        //获取当前用户信息
        SysUser details = SysUserUtils.getSysUser();
        //获取时间
        LocalDateTime localDateTime = LocalDateTime.now();
        //通过构造参数设置值
        SsAttendance appSsAttendance = new SsAttendance(
        		details,DateUtil.format(localDateTime,"yyyy-MM-dd HH:mm:ss"));
        //copy值到实体
        BeanUtils.copyProperties(dto, appSsAttendance);
        Map<String,Object> car = carService.getCarListByUserId(details.getId());
        appSsAttendance.setIdentity(details.getRoleName());
        appSsAttendance.setCarNo(car.get("car_no").toString());
        boolean save = attendanceService.saveEntity(appSsAttendance);
        if (save){
        	return Messages.success(DateUtil.format(localDateTime,"yyyy-MM-dd HH:mm:ss"));
        }
        return Messages.failed("打卡失败!");
    }

    /**
     * 修改智慧工地-作业记录表
     * @param appSsAttendance 智慧工地-作业记录表
     * @return R
     **/
    @ApiOperation(value="修改智慧工地-作业记录表",notes="修改智慧工地-作业记录表")
    @PutMapping
    public R<Boolean> updateById(@RequestBody SsAttendance appSsAttendance) {
        return R.ok(attendanceService.updateEntity(appSsAttendance));
    }

    /**
     * 通过id删除智慧工地-作业记录表
     * @param id id
     * @return R
     **/
    @ApiOperation(value="通过id删除智慧工地-作业记录表",notes="通过id删除智慧工地-作业记录表")
    @DeleteMapping("/{id}")
    public R<Boolean> removeById(@PathVariable Long id) {
        return R.ok(attendanceService.deleteById(id));
    }
    
}